import React, { useState, useMemo, useEffect } from 'react';
import BasicLayout from './BasicLayout';
import UserLayout from './UserLayout';
import SubNavLayout from './SubNavLayout';
import SecurityLayout from './SecurityLayout';
import Authorized from '@/components/Authorized/index';
import { cancelOrKeep, favoriteMenu } from '@/pages/user/index/service';
import { useRequest } from '@umijs/hooks';
import styles from './BasicLayout.less';

import { AliveScope } from 'react-activation'

export default function Layout(props) {
  const { location } = props;
  const { pathname } = location;

  const changeCancelOrKeep = (item) => {
    const { id, name, path } = item;
    runCancelOrKeep({ id, name, path });
  }


  let menuData = [], localFavoriteMenus = [];
  try {
    menuData = JSON.parse(localStorage.getItem('resource')) || [];
  } catch (error) {
    console.error(error);
  }
  const appMenus = useMemo(() => {
    return menuData.map(({ children, ...rest }) => ({ ...rest }));
  }, [menuData]);

  if (/\/user\/login/i.test(pathname)) {
    return <UserLayout {...props} />;
  }

  const renderChild = children => {
    const noSecondaryNavigation = /\/messageCenter|\/barChartCenter/i;
    if (noSecondaryNavigation.test(pathname)) {
      return children;
    } else {
      return <SubNavLayout menuData={menuData}  {...props} changeCancelOrKeep={changeCancelOrKeep} />;
    }
  };

  return (
    <AliveScope>
      <SecurityLayout>
        <Authorized pathname={pathname} menuData={menuData}>
          <BasicLayout {...props} appMenus={menuData}>
            {renderChild(props.children)}
          </BasicLayout>
        </Authorized>
        <p className={styles.footerDescription}>
          四川航空股份有限公司 蜀ICP备18038377号-1 版本 PSIS_1.1.2_20210722_release 
          <a href="https://beian.miit.gov.cn" target="_blank">&nbsp;&nbsp;&nbsp;蜀ICP备18038377号-1</a>
        </p>
      </SecurityLayout>
    </AliveScope>
  );
}
